using System;
using System.Web.UI;
using System.Web.Compilation;
using System.CodeDom;


/// <summary>
/// Summary description for BrowserSwitchExpressionBuilder
/// </summary>
[ExpressionPrefix("BrowserSwitch")]
public class BrowserSwitchExpressionBuilder : ExpressionBuilder
{
	public BrowserSwitchExpressionBuilder()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        if ((entry.DeclaringType == null) || (entry.PropertyInfo == null))
        {
            return new 
                CodeMethodInvokeExpression(new CodeTypeReferenceExpression(base.GetType()), 
                "GetIsIE", new CodeExpression[] { 
                    new CodePrimitiveExpression(entry.Expression.Trim()) });
        }
        return new 
            CodeMethodInvokeExpression(new CodeTypeReferenceExpression(base.GetType()), 
            "GetIsIE", new CodeExpression[] { 
                new CodePrimitiveExpression(entry.Expression.Trim()), 
                new CodeTypeOfExpression(entry.DeclaringType), 
                new CodePrimitiveExpression(entry.PropertyInfo.Name) });
    }
    public static object GetIsIE(string key, Type targetType, string propertyName)
    {
        return System.Web.HttpContext.Current.Request.Browser.Browser == "IE";
    }
    public static object GetIsIE(string key)
    {
        return System.Web.HttpContext.Current.Request.Browser.Browser == "IE";
    }
    public override bool SupportsEvaluate
    {
        get
        {
            return true;
        }
    }
    public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        return BrowserSwitchExpressionBuilder.GetIsIE(entry.Expression, target.GetType(), entry.PropertyInfo.Name);
    }

}
